//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Extrapolator.h"
using namespace Cephei::QL::Math::Interpolations;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Interpolations::CExtrapolator::CExtrapolator () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phExtrapolator = NULL;
#endif
        _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (new QuantLib::Extrapolator ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Interpolations::CExtrapolator::CExtrapolator (boost::shared_ptr<QuantLib::Extrapolator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExtrapolator = NULL;
#endif
	_ppExtrapolator = &childNative;
    
}
Cephei::QL::Math::Interpolations::CExtrapolator::CExtrapolator (QuantLib::Extrapolator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phExtrapolator = NULL;
#endif
	_ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (&childNative);
    
    _ExtrapolatorOwner = owner;
    
}

Cephei::QL::Math::Interpolations::CExtrapolator::CExtrapolator (CExtrapolator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExtrapolator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Interpolations::CExtrapolator::CExtrapolator (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExtrapolator = NULL;
#endif
	if (!t->IsSubclassOf(CExtrapolator::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Interpolations::CExtrapolator::CExtrapolator (QuantLib::Handle<QuantLib::Extrapolator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phExtrapolator = &childNative;
	_ppExtrapolator = &static_cast<boost::shared_ptr<QuantLib::Extrapolator>>(childNative.currentLink());
    
    _ExtrapolatorOwner = owner;
}
Cephei::QL::Math::Interpolations::CExtrapolator::CExtrapolator (QuantLib::Handle<QuantLib::Extrapolator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phExtrapolator = &childNative;
	_ppExtrapolator = &static_cast<boost::shared_ptr<QuantLib::Extrapolator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Interpolations::CExtrapolator::CExtrapolator (QuantLib::Extrapolator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExtrapolator = NULL;
#endif
	_ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (new QuantLib::Extrapolator (childNative));
    
}
#endif

Cephei::QL::Math::Interpolations::CExtrapolator::~CExtrapolator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppExtrapolator != NULL)
    {
	    delete _ppExtrapolator;
        _ppExtrapolator = NULL;
    }
}
Cephei::QL::Math::Interpolations::CExtrapolator::!CExtrapolator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppExtrapolator != NULL)
    {
	    delete _ppExtrapolator;
    }
}
QuantLib::Extrapolator& Cephei::QL::Math::Interpolations::CExtrapolator::GetReference ()
{
    if (_ppExtrapolator == NULL) throw REFNEW NativeNullException ();
	return **_ppExtrapolator;
}
boost::shared_ptr<QuantLib::Extrapolator>& Cephei::QL::Math::Interpolations::CExtrapolator::GetShared ()
{
    if (_ppExtrapolator == NULL) throw REFNEW NativeNullException ();
	return *_ppExtrapolator;
}
QuantLib::Extrapolator* Cephei::QL::Math::Interpolations::CExtrapolator::GetPointer ()
{
    if (_ppExtrapolator == NULL) throw REFNEW NativeNullException ();
	return &**_ppExtrapolator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Extrapolator>& Cephei::QL::Math::Interpolations::CExtrapolator::GetHandle ()
{
	if (_phExtrapolator == NULL)
	{
		_phExtrapolator = new Handle<QuantLib::Extrapolator> (*_ppExtrapolator);
	}
	return *_phExtrapolator;
}
#endif
bool Cephei::QL::Math::Interpolations::CExtrapolator::HasNative () 
{
	return (_ppExtrapolator != NULL);
}

Boolean Cephei::QL::Math::Interpolations::CExtrapolator::AllowsExtrapolation::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppExtrapolator)->allowsExtrapolation ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Interpolations::IExtrapolator^ Cephei::QL::Math::Interpolations::CExtrapolator::DisableExtrapolation (Microsoft::FSharp::Core::FSharpOption<Boolean>^ b)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        bool _b = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (b) ? (bool)ValueHelper::Convert (b->Value) : true); //9a
    	(*_ppExtrapolator)->disableExtrapolation ( _b );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Interpolations::IExtrapolator^ Cephei::QL::Math::Interpolations::CExtrapolator::EnableExtrapolation (Microsoft::FSharp::Core::FSharpOption<Boolean>^ b)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        bool _b = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (b) ? (bool)ValueHelper::Convert (b->Value) : true); //9a
    	(*_ppExtrapolator)->enableExtrapolation ( _b );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Interpolations::IExtrapolator^ Cephei::QL::Math::Interpolations::CExtrapolator_Factory::Create ()
{
    return REFNEW CExtrapolator ();
}
